#!/bin/bash
#mail:sopcce@qq.com
#data:2022年1月27日15:29:21
#自动初始化安装 Fastdfs
clear
echo -e '>>>>>> README <<<<<<
\033[32m#################################################\r\n
##          Auto Install Fastdfs               ##\r\n
##          Press Ctrl + C to cancel           ##\r\n
##          Any key to continue                ##\r\n
#################################################\033[0m'


##  软件下载路径
download_file_path="/usr/local"
download_file_url="https://gitee.com/sopcce/shell/raw/master/2.fastdfs/zip/"
download_file_libfastcommon='libfastcommon-1.0.7.tar.gz'
download_file_nginx='nginx-1.16.1.tar.gz'
download_file_FastDFS='FastDFS_v5.05.tar.gz'
download_file_nginxFS='fastdfs-nginx-module_v1.16.tar.gz'

download_file_libfastcommon_fileName='libfastcommon-1.0.7'
download_file_nginx_fileName='nginx'
download_file_FastDFS_fileName='FastDFS'
download_file_nginxFS_fileName='fastdfs-nginx-module'
fastdfs_path="/etc/fdfs/"
fastdfs_web_port=8800


# 获取系统信息
sys_version=`cat /etc/redhat-release |awk '{print $4}'|cut -d. -f1`
IP=`ip addr|grep "inet "|grep -v 127.0.0.1|awk '{print $2}'|cut -d/ -f1`
jvm_conf="/usr/local/elasticsearch/config/jvm.options"
sys_mem=`free -m|grep Mem:|awk '{print $2}'|awk '{sum+=$1} END {print sum/1024}'|cut -d. -f1`




wget_download_file() {

  echo -e '\033[32m#################################################\r\n
##                  初始化下载文件               ##\r\n
#################################################\033[0m'

  if [ ! -d ${download_file_path} ];then
    mkdir -p ${download_file_path} && cd ${download_file_path}
  else
    cd ${download_file_path}
  fi

  wget -P ${download_file_path} -O ${download_file_libfastcommon} ${download_file_url}${download_file_libfastcommon}
  wget -P ${download_file_path} -O ${download_file_nginx} ${download_file_url}${download_file_nginx}
  wget -P ${download_file_path} -O ${download_file_FastDFS} ${download_file_url}${download_file_FastDFS}
  wget -P ${download_file_path} -O ${download_file_nginxFS} ${download_file_url}${download_file_nginxFS}


  echo -e '\033[32m#################################################\r\n
##                 下载文件完成                 ##\r\n
#################################################\033[0m'
  sleep 3
}

install_libfastcommon(){

   echo -e '\033[32m#################################################\r\n
##           安装libfastcommon 开始             ##\r\n
#################################################\033[0m'

  # 安转 libfastcommon
  cd ${download_file_path}
  tar -zxvf ${download_file_libfastcommon} -C ${download_file_path}
  cd ${download_file_libfastcommon_fileName}
  ./make.sh
  ./make.sh install
  cd /usr/lib64
  cp libfastcommon.so /usr/lib



  echo -e '\033[32m#################################################\r\n
##           安装libfastcommon 完成             ##\r\n
#################################################\033[0m'

  sleep 5

}
install_fastdfs(){

  echo -e '\033[32m#################################################\r\n
##          m安装 fastdfs 开始                  ##\r\n
#################################################\033[0m'
  cd ${download_file_path}
  tar -zxvf ${download_file_FastDFS} -C ${download_file_path}
  cd ${download_file_FastDFS_fileName}
  ./make.sh
  ./make.sh install
  cd conf
  cp * ${fastdfs_path}

   echo -e '\033[32m#################################################\r\n
##          安装 fastdfs 完成                   ##\r\n
#################################################\033[0m'
  sleep 5

}
set_fastdfs_config(){
  echo -e "=================================================================="
  echo -e "\033[32m配置 fastdfs 开始  \033[0m"
  echo -e "=================================================================="
  cd ${fastdfs_path}
  cp tracker.conf.sample tracker.conf
  cp storage.conf.sample storage.conf
  cp client.conf.sample client.conf

  sleep 2
  cat tracker.conf

  old_str='base_path=/home/yuqing/fastdfs'
  new_str='base_path=/home/fastdfs'
  sed -i 's#'''$old_str'''#'''$new_str'''#g' tracker.conf

  old_str='http.server_port=8080'
  new_str='http.server_port=8000'
  sed -i 's#'''$old_str'''#'''$new_str'''#g' tracker.conf

  sleep 2
  cat storage.conf

  old_str='base_path=/home/yuqing/fastdfs'
  new_str='base_path=/home/fastdfs'
  sed -i 's#'''$old_str'''#'''$new_str'''#g' storage.conf

  old_str='store_path0=/home/yuqing/fastdfs'
  new_str='store_path0=/home/fastdfs_storage'
  sed -i 's#'''$old_str'''#'''$new_str'''#g' storage.conf


  old_str='tracker_server=192.168.209.121:22122'
  new_str='tracker_server='${IP}':22122'
  sed -i 's#'''$old_str'''#'''$new_str'''#g' storage.conf

  old_str='http.server_port=8888'
  new_str='http.server_port=8800'
  sed -i 's#'''$old_str'''#'''$new_str'''#g' storage.conf

  sleep 2
  cat client.conf

    old_str='base_path=/home/yuqing/fastdfs'
  new_str='base_path=/home/fastdfs'
  sed -i 's#'''$old_str'''#'''$new_str'''#g' client.conf


  old_str='tracker_server=192.168.0.197:22122'
  new_str='tracker_server='${IP}':22122'
  sed -i 's#'''$old_str'''#'''$new_str'''#g' client.conf


  old_str='http.server_port=8080'
  new_str='http.server_port=8800'
  sed -i 's#'''$old_str'''#'''$new_str'''#g' client.conf



  sleep 3



  /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
  /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

  sleep 3
  /usr/bin/fdfs_test /etc/fdfs/client.conf upload /etc/fdfs/anti-steal.jpg


  echo -e "=================================================================="
  echo -e "\033[32m 配置 fastdfs 完成  \033[0m"
  echo -e "=================================================================="
}
system_init(){
  sleep 3
  echo -e "=================================================================="
  echo -e "\033[32m系统初始化 开始  \033[0m"
  echo -e "=================================================================="
  # mkdir file
  yum install wget git unzip zip gcc gcc-c++ make cmake automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel libevent vim -y



  mkdir -p /home/fastdfs/
  mkdir -p /home/fastdfs_storage
  mkdir -p /home/fastdfs_client
  mkdir -p /var/temp/nginx/client
  mkdir -p /usr/local/nginx/logs
  mkdir -p /usr/local/src
  mkdir -p ${fastdfs_path}



  echo -e "=================================================================="
  echo -e "\033[32m系统初始化 完成  \033[0m"
  echo -e "=================================================================="

}
system_check(){
  sleep 3
	MYSQLD_CHECK=$(ps -ef |grep mysqld|grep -v grep|grep -v /www/server/mysql)
	PHP_CHECK=$(ps -ef|grep php-fpm|grep master|grep -v /www/server/php)
	NGINX_CHECK=$(ps -ef|grep nginx|grep master|grep -v /www/server/nginx)
	HTTPD_CHECK=$(ps -ef |grep -E 'httpd|apache'|grep -v /www/server/apache|grep -v grep)
	if [ "${PHP_CHECK}" ] || [ "${MYSQLD_CHECK}" ] || [ "${NGINX_CHECK}" ] || [ "${HTTPD_CHECK}" ];then
		install_check
  else
      echo -e "\033[32m软件初始化完成，不存在安装软件 \033[0m"
	fi
}
install_check(){
	if [ "${INSTALL_FORCE}" ];then
		return
	fi
	echo -e "----------------------------------------------------"
	echo -e "检查已有其他Web/mysql环境，安装宝塔可能影响现有站点及数据"
	echo -e "----------------------------------------------------"
	echo -e "已知风险/Enter yes to force installation"
	read -p "输入yes强制安装: " yes;
	if [ "$yes" != "yes" ];then
		echo -e "------------"
		echo "取消安装"
		exit 1;
	fi
	INSTALL_FORCE="true"
}
get_ip_address(){

	IP=$(curl -sS --connect-timeout 10 -m 60 https://www.bt.cn/Api/getIpAddress)
  sleep 3
  echo -e "\033[32mIP地址：  ${IP} \r\n
================================================================== \033[0m"
}

# 主函数统一调用
Install_Main() {
  	startTime=`date +%s`
    get_ip_address

    system_init
    # 检测系统软件
    system_check

    # 下载文件
    wget_download_file


    # 解压文件
    install_libfastcommon
    sleep 3
    install_fastdfs
    sleep 3
    set_fastdfs_config




    echo "url:http://$IP:5601"


}
# 执行主函数
Install_Main
